local Screen = require "Screen"

local M = {}

local DrawDebugLine = UE.UKismetSystemLibrary.DrawDebugLine
local DrawDebugPoint = UE.UKismetSystemLibrary.DrawDebugPoint
local DrawDebugSphere = UE.UKismetSystemLibrary.DrawDebugSphere

function M.DrawDebugLine(Ctx, Start, End, color, duration, thick)
  color = color or UE.FLinearColor(0, 1, 0, 1)
  duration = duration or 5
  thick = thick or 0.1

  DrawDebugLine(Ctx, Start, End, color, duration, thick)
end


function M.DrawDebugPoint(Ctx, Pos, color, size, duration)
  color = color or UE.FLinearColor(0, 1, 0, 1)
  size = size or 5
  duration = duration or 5

  M.Print(Pos)
  
  DrawDebugPoint(Ctx, Pos, size, color, duration)
end

function M.DrawDebugSphere(Ctx, Center, Radius, Segments, color, duration, thick)
  Radius = Radius or 1
  Segments = Segments or 1
  color = color or UE.FLinearColor(0, 1, 0, 1)
  duration = duration or 5
  thick = thick or 0.5

  DrawDebugSphere(Ctx, Center, Radius, Segments, color, duration, thick)
end

function M.DrawImpactNormal(Ctx, HitResult, length)
  length = length or 50

  local Start = HitResult.ImpactPoint
  local End = UE.UKismetMathLibrary.Add_VectorVector(Start, HitResult.ImpactNormal*length)
  DrawDebugLine(Ctx, Start, End, UE.FLinearColor(1, 0, 0, 1), 1, 0.5)
end


function M.PrintActorName(Actor)
  Screen.Print(UE.UKismetSystemLibrary.GetDisplayName(Actor))
end

function M.PrintActorOName(Actor)
  Screen.Print(UE.UKismetSystemLibrary.GetObjectName(Actor))
end

function M.PrintFVector(Vec)
  Screen.Print(string.format("X: %f, Y: %f, Z: %f", Vec.X, Vec.Y, Vec.Z))
end

function M.PrintFRotator(Rot)
  Screen.Print(string.format("Roll: %f, Pitch: %f, Yaw: %f", Rot.Roll, Rot.Pitch, Rot.Yaw))
end

function M.Print(Obj, prefix)
  -- Screen.Print('-----------------------')
  -- Screen.Print(type(Obj.Z))
  -- Screen.Print(string.format("X: %s", type(Obj.X)))
  -- Screen.Print(string.format("Roll: %s", type(Obj.Roll)))
  -- Screen.Print(string.format("GetClass: %s", type(Obj.GetClass)))

  if type(Obj) == 'boolean' then
    prefix = prefix or 'Boolean'
    if Obj then
      Screen.Print(prefix..': true')
    else
      Screen.Print(prefix..': false')
    end
  elseif type(Obj) == 'number' then
    prefix = prefix or 'Num: '
    Screen.Print(prefix .. Obj)
  elseif type(Obj.X) == 'number' then
    if type(Obj.Z) == 'number' then
      Screen.Print(string.format("X: %f, Y: %f, Z: %f", Obj.X, Obj.Y, Obj.Z))
    else
      Screen.Print(string.format("X: %f, Y: %f", Obj.X, Obj.Y))
    end
  elseif type(Obj.Roll) == 'number' then
    Screen.Print(string.format("Roll: %f, Pitch: %f, Yaw: %f", Obj.Roll, Obj.Pitch, Obj.Yaw))
  elseif type(Obj.GetClass) == 'function' then
    prefix = prefix or 'Class: '
    Screen.Print(prefix .. Obj:GetClass():GetName())
  else
    Screen.Print('Unknown')
  end
end

function M.PrintTbl(userdata)
  Screen.Print('---------- Table Begin ----------')
  for k,v in pairs(getmetatable(userdata)) do
    print(tostring(k),tostring(v))
    Screen.Print(tostring(k) .. ':  ' .. tostring(v))
  end
  Screen.Print('---------- Table end ----------')
end


return M
